<?php
/**
 * @name OrderController
 * @author xuebingwang
 * @desc 商城订单控制器
 * @see http://www.php.net/manual/en/class.yaf-controller-abstract.php
*/
class OrderController extends MemberController {

    public function deleteAction($sn=''){

        if(empty($sn)){
            $this->error('订单编号不能为空!');
        }

        $curl = new Curl();
        $resp = $curl->setData(['sn'=>$sn,'unionid'=>$this->user['unionid']])
            ->send('mall/order/delete');

        if(empty($resp) || $resp['errcode'] != 0){
            $this->error($resp['errmsg']);
        }else{
            $this->success($resp['errmsg']);
        }
    }

    public function closeAction($sn=''){

        if(empty($sn)){
            $this->error('订单编号不能为空!');
        }

        $curl = new Curl();
        $resp = $curl->setData(['sn'=>$sn,'unionid'=>$this->user['unionid']])
            ->send('mall/order/close');

        if(empty($resp) || $resp['errcode'] != 0){
            $this->error($resp['errmsg']);
        }else{
            $this->success($resp['errmsg']);
        }
    }

    function indexAction($type='all'){

        $cur_page = intval($this->getRequest()->getQuery('page',1));

        $curl = new Curl();
        $pagenum = $this->config->application->pagenum;
        $data = [
                    'unionid'=>$this->user['unionid'],
                    'type'=>$type,
                    'pagenum'=>$pagenum,
                    'page'=>$cur_page,
                ];
        $resp = $curl->setData($data)->send('mall/order/getList');
        $list = [];
        $total = ['all'=>0,'wait'=>0,'comment'=>0,'activate'=>0];
        if(!empty($resp) && $resp['errcode'] == 0){
            $list = $resp['list'];
            $total = $resp['total'];
        }

        $page = new Page($total[$type],$pagenum,$_REQUEST);

        $this->getView()->assign('page',$page->show());

        $this->getView()->assign('type',$type);
        $this->getView()->assign('total',$total);
        $this->getView()->assign('list',$list);

        $info = M('t_wx_user')->get(
            [
                'userid',
                'unionid',
                'qr_ticket',
                'qr_expire_seconds',
                'qr_create_time',
            ],
            ['userid'=>$this->user['userid']]
        );
        $qrcode = get_wx_qrcode($this->wechat,$info);
        $this->getView()->assign('qrcode',$qrcode);
        $this->getView()->assign('cpsid',$this->user['userid']);
    }

    public function viewAction($sn=''){

        if(empty($sn)){
            $this->error('订单编号不能为空!');
        }

        $curl = new Curl();
        $order = $curl->setData(['sn'=>$sn,'unionid'=>$this->user['unionid'],'need_p'=>1,'need_a'=>1])->send('mall/order/getDetail');

        if(empty($order) || $order['errcode'] != 0){
            $this->error('订单不存在!');
        }

        $model = new Model('t_payment_setting');
        $this->getView()->assign('payment',$model->get('name',['code'=>$order['pay_code']]));
        $this->getView()->assign('item',$order);
    }

    public function waitAction($sn=''){

        if(empty($sn)){
            $this->error('订单编号不能为空!');
        }

        $curl = new Curl();
        $order = $curl->setData(['sn'=>$sn,'unionid'=>$this->user['unionid'],'need_p'=>1,'need_a'=>1])->send('mall/order/getDetail');

        if(empty($order) || $order['errcode'] != 0){
            $this->error('订单不存在!');
        }

        $info = M('t_wx_user')->get(
            [
                'userid',
                'unionid',
                'qr_ticket',
                'qr_expire_seconds',
                'qr_create_time',
            ],
            ['userid'=>$this->user['userid']]
        );
        $qrcode = get_wx_qrcode($this->wechat,$info);
        $this->getView()->assign('qrcode',$qrcode);

        $this->getView()->assign('order',$order);
        $this->getView()->assign('cpsid',$this->user['userid']);
    }

    public function remarkAction($sn=''){
        if(empty($sn)){
            $this->error('订单号不能为空!');
        }

        $data = ['sn'=>$sn,'unionid'=>$this->user['unionid']];

        $data['product_id'] = intval($this->getRequest()->getPost('product_id'));
        if(empty($data['product_id'])){
            $this->error('产品ID不能为空');
        }
        $data['rate'] = intval($this->getRequest()->getPost('rate',1));

        $data['remark'] = trim($this->getRequest()->getPost('remark'));
        if(strlen($data['remark']) > 300){
            $this->error('评价内容不能为空!');
        }

        $curl = new Curl();
        $resp = $curl->setData($data)->send('mall/order/remark');

        if(empty($resp)){
            $this->error('评价失败,请重新再试!');
        }elseif($resp['errcode'] != 0){
            $this->error($resp['errmsg']);
        }else{
            $this->success($resp['errmsg']);
        }
    }

    public function activateAction($sn=''){

        $curl = new Curl();

        if(IS_POST){

            $consignee  = $this->getRequest()->getPost('consignee');
            $mobile     = $this->getRequest()->getPost('mobile');
            $idnum      = $this->getRequest()->getPost('idnum');
            $address_id = $this->getRequest()->getPost('address_id');
            $sn         = $this->getRequest()->getPost('order_sn');

            $cate_id    = $this->getRequest()->getPost('cate_id');

            if($cate_id != '31' && $cate_id != '33'){
                if(empty($address_id) && (empty($consignee) || empty($mobile) || empty($idnum))){
                    $this->error('没有收货地址！');
                }
            }else{

                if(empty($mobile)){
                    $this->error('手机号码不能为空！');
                }
            }

            $data = [
                'unionid'=>$this->user['unionid'],
                'sn'=>$sn,
                'address_id'=>$address_id,
                'consignee'=>$consignee,
                'mobile'=>$mobile,
                'idnum'=>$idnum,
            ];
            $resp = $curl->setData($data)->send('mall/order/activate');
            if(empty($resp) || $resp['errcode'] !== 0){
                $this->error(isset($resp['errmsg']) ? $resp['errmsg'] : '激活失败，未知错误，请联系管理员！');
            }else{
                $this->success('激活成功!','/article/info/id/25.html');
            }
        }
        $order = $curl->setData(['sn'=>$sn,'unionid'=>$this->user['unionid'],'need_p'=>1])->send('mall/order/getDetail');

        if(empty($order) || $order['errcode'] != 0){
            $this->error('订单不存在!','/member/order/index.html');
        }
//
//        if($order['status'] == OrderModel::STATUS_COMPLETED){
//            $this->error('该订单已经激活!','/member/order/index.html');
//        }

        if($order['status'] != OrderModel::STATUS_WATI_ACTIVE){
            $this->error('该订单不能激活!','/member/order/index.html');
        }

        $address = $curl->setData(['unionid'=>$this->user['unionid']])->send('mall/user/getAddress');

        $address = array_merge(['id'=>'','consignee'=>'' ,'mobile'=>'','idnum'=>''],$address);

        $product = current($order['products']);
        $cate_id = M('t_product')->get('category_id',['id'=>$product['product_id']]);
        $this->getView()->assign('cate_id',$cate_id);

        $this->getView()->assign('order',$order);

        $this->getView()->assign('address',$address);
    }
}
